#ifndef AES_TCP_H
#define AES_TCP_H

#include <QtNetwork>
#include <QDebug>
#include <QObject>
#include <QtXml>
#include <QtXml/QDomDocument>
#include <QPixmap>
#include "qt_aes.h"

#define ENCRYPT_YES 0x12
#define ENCRYPT_NO	0x7

#define BIG		1
#define LITTLE	2

enum DOWN_STATUS{

    DOWN_ING= 0,
    DOWN_OK
};

struct lus_msg{

    QByteArray msg;
    QByteArray to_addr;
    quint8 encrypt_c;
};

class aes_tcp_thread: public QThread
{
  //  void run();
};

class aes_tcp : public QObject
{
   Q_OBJECT
public:
     ~aes_tcp();

    /*在调用aes_tcp_init之前设置参数*/
    QString server_ip;

    quint16 msg_port=0;
    quint16 big_port=0;

    bool debug_switch = false;

    /*在调用aes_tcp_init之后设置参数*/

    QString to_addr;

    /****************************/


    QString fix_id;
    QString from_server_id;
    QString lus_group_id;
    QString lus_version;


    UCHAR aes_key[16] = {0x22, 0xa2, 0x13, 0xe2, 0x22, 0xa2, 0x12, 0x20, 0x22, 0x15, 0x22, 0xda, 0x67, 0x22, 0x32, 0x22 };
    TAesClass *aes = new TAesClass;

    uint8_t down_status = DOWN_OK;

    QTcpSocket *msg_tcp_socket;  //命令传输

    void aes_tcp_init(void);
    void aes_tcp_close(void);
    void lus_set_order(qint8 order);
    void update_fix_id();
    void sendMessage(QByteArray b_data, quint8 encrypt_c);
    void prase_terminal_cmd(QDomElement rootnode);
    void lus_send_user_msg(QString type, QString to_addr, QString body);
    void lus_send_raw_msg(QByteArray b_data, quint8 encrypt_c);
    void lus_down_pool_file(QString index, QString label, QString file_name);
private:

    void newConnect(); //连接服务器
    void default_do_cmd(QString cmd);
    void default_do_big_cmd(QString cmd);


    QTcpSocket *big_tcp_socket;  //图像等高占用率传输
    QTcpSocket	*f_tcpSocket;	  //传输文件

    qint8 byte_order = LITTLE;
    char * tcp_big_data_cache;
    char * tcp_msg_data_cache;

    /*文件传输*/
    void newFileConnect();
    void recive_file_finally(void);
    QString get_file_md5(QString str_path);

    void do_send_msg(QTcpSocket *socket,QByteArray b_data, quint8 encrypt_c);
    QString message;  //存放从服务器接收到的字符串
    quint32 msg_blockSize=0; //存放文件的大小信息
    quint32 big_blockSize=0; //存放文件的大小信息

     /*文件传输变量*/
    qint64 file_type=0;
    qint64 totalBytes=0;  //存放总大小信息
    qint64 bytesReceived=0;  //已收到数据的大小
    qint64 fileNameSize=0;  //文件名的大小信息
    QString fileName;   //存放文件名
    QFile *localFile;   //本地文件
    QByteArray inBlock;   //数据缓冲区
    QString s_file_md5;

     QTimer *timer_connect;
     QTimer *timer_heart;

private slots:

      void displayError(QAbstractSocket::SocketError);  //显示错误
      void readMessage_msg();
      void readMessage_big();
     // void readMessage(uint8_t select);
        void f_connected(void);
      void enable_reconnect(void);
      void disable_reconnect(void);
      void timer_auto_connect(void);
      void timer_heart_keep(void);

    void updateServerProgress();

signals:

     void aes_tcp_connected(void);
     void aes_tcp_disconnected(void);

     void read_xml_msg(QDomElement rootnode);
     void read_msg(QByteArray msg);
     void recive_lus_pack(QString file_name);
     void image_ready(QImage *image);
     void down_status_change(uint8_t status);
};

#endif // AES_TCP_H
